package com.ilove.friend.model;

import java.io.IOException;
import org.kalmeo.kuix.widget.Gauge;

import org.kalmeo.kuix.core.model.DataProvider;

import com.ilove.model.DataAccess;

public class FriendDataProvider extends DataProvider {
	// server 210.211.99.58:6868
	// http://117.6.131.167
	String provincelist = "http://10h.vn:6868/AppServlet/servlet/ProvinceList?";
	String friendlist = "http://10h.vn:6868/AppServlet/servlet/FriendList?";
	String friend = "http://10h.vn:6868/AppServlet/servlet/Friend?";
	String HotNewlist = "http://10h.vn:6868/AppServlet/servlet/HotNewList?";
	String urlupload = "http://10h.vn:6868/Upload";

	private String info;

	private static final String INFO_PROPERTY = "info";

	public void setInfo(String info) {
		this.info = info;
		dispatchUpdateEvent(INFO_PROPERTY);
	}

	protected Object getUserDefinedValue(String property) {

		if (INFO_PROPERTY.equals(property)) {
			return this.info;
		}
		return null;
	}

	public String[] getProvinceList() throws Exception {

		String[] result = null;
		String url = provincelist;
		String sTopic = DataAccess.getData(url);
		if (!"".equalsIgnoreCase(sTopic.trim()))
			result = DataAccess.split(sTopic, "\n");

		return result;
	}

	public int getTotalProvince() throws Exception {

		int total = 1;
		String url = provincelist + "total=1";
		System.out.println(url);
		String sTopic = DataAccess.getData(url);

		if (!"".equalsIgnoreCase(sTopic)) {
			total = Integer.parseInt(sTopic.trim());
			System.out.println(total);
		}
		return total;
	}

	public String[] getProvinceListWithPage(int currentpage, int pagesize)
			throws Exception {

		String[] result = null;
		String url = provincelist + "page=" + currentpage + "&pagesize="
				+ pagesize;
		String sTopic = DataAccess.getData(url);
		if (!"".equalsIgnoreCase(sTopic.trim()))
			result = DataAccess.split(sTopic, "\n");

		return result;
	}

	
	public void setCategoryList(String[] list, Gauge progressGauge) {
		removeAllItems(FriendContants.PROVINCELISTS_PROPERTY);
		int nchars = -1;
		String code = "";
		String name = "";
		do {
			nchars++;
			String data = list[nchars];
			code = data.substring(0, data.indexOf("@@@"));
			name = data.substring(data.indexOf("@@@") + 3, data.length() - 1);
			ProvinceList item = new ProvinceList();
			item.code = code;
			item.name = name;
			addItem(FriendContants.PROVINCELISTS_PROPERTY, item);
			progressGauge.setValue(nchars);
		} while (nchars < list.length - 1);
	}

	// Friend list process
	public String[] getFriendList(String provincecode, String sexcode,
			int currentpage, int pagesize) throws Exception {

		String[] result = null;
		String url = friendlist + "province=" + provincecode.trim() + "&sex="
				+ sexcode.trim() + "&page=" + currentpage + "&pagesize="
				+ pagesize;
		String sTopic = DataAccess.getData(url);
		if (!"".equalsIgnoreCase(sTopic.trim()))
			result = DataAccess.split(sTopic, "\n");
		return result;
	}

	public int getTotalFriend(String provincecode, String sexcode)
			throws Exception {
		int total = 1;
		String url = friendlist + "total=1&province=" + provincecode.trim()
				+ "&sex=" + sexcode.trim();
		String sTopic = DataAccess.getData(url);
		if (!"".equalsIgnoreCase(sTopic)) {
			total = Integer.parseInt(sTopic.trim());
			System.out.println(total);
		}
		return total;
	}

	public void setFriendList(String[] list) {
		removeAllItems(FriendContants.FRIENDLISTS_PROPERTY);
		for (int i = 0; i < list.length - 1; i++) {
			FriendList objfriend = new FriendList();
			String[] frienddata = DataAccess.split(list[i], "@@@");
			objfriend.code = frienddata[0];
			objfriend.name = frienddata[1];
			if (!"".equalsIgnoreCase(frienddata[2].trim())) {
				try {
					objfriend.previewimage = DataAccess.getImage(urlupload
							+ frienddata[2].trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			addItem(FriendContants.FRIENDLISTS_PROPERTY, objfriend);
		}
	}

	// get Friend
	public FriendList getFriend(String code) throws Exception {
		String sTopic = "";
		String url = friend + "code=" + code.trim();
		System.out.println(url);
		sTopic = DataAccess.getData(url);
		FriendList item = new FriendList();
		if (!"".equalsIgnoreCase(sTopic)) {
			String[] frienddata = DataAccess.split(sTopic, "@@@");
			System.out.println(frienddata[0] + frienddata[1] + frienddata[2]
					+ frienddata[3]);
			item.code = frienddata[0];
			item.name = frienddata[1];
			item.previewimage = DataAccess.getImage(urlupload
					+ frienddata[2].trim());
			item.info = frienddata[3];
		}
		return item;
	}

	// hot new list
	// Friend list process
	public String[] getHotNewFriendList(String provincecode, String sexcode,
			String type) throws Exception {
		String[] result = null;
		String url = friendlist + "province=" + provincecode.trim() + "&sex="
				+ sexcode.trim() + "&type" + type.trim();
		String sTopic = DataAccess.getData(url);
		if (!"".equalsIgnoreCase(sTopic.trim()))
			result = DataAccess.split(sTopic, "\n");
		return result;
	}
}
